//统一设置图片样式
function setTabImg(){
	var w = $("#tabDiv").width();
	var w1 = (w-390);
	var h1 = w1*0.62;
	$(".tabImg").css("width",w1);
	$(".tabImg").css("height",h1);
	$(".tabImg1").css("width",(w-250)*0.75);
	$(".tabImg1").css("height",(w-250)*0.75*0.62);
	$(".remarkDiv1").css("width",(w-250)*0.75-20);
	$(".remarkDiv").css("width",(w-390)-20);
}

function add_input_file(tbfile, index, commonName) {
	var temp = $("<tr style='border:1px #98a9b8 solid;'><td class='inquire_form' colspan='3'><input style='width:400px;' class='easyui-validatebox textfield9' validType='v_img'  name='"+commonName+"' type='file'/><input style='margin-top:5px;margin-left:10px;font-size:14px;padding:3px;'  type='button' class='deleteBtn' value='删除' /></td></tr>");
	tbfile.append(temp);
	$(".deleteBtn").click(function() {
		$(this).parent().parent("tr").remove();
	});
}

function deleteOne() {
	$(".deleteBtn").click(function() {
		$(this).parent().parent("tr").remove();
	});
}

function setSelectValidate(){
	//为select添加非空验证
	$("select").addClass('easyui-validatebox textfield88');
	$("select").attr("required","");
	$.parser.parse($('#edit_form').parent().parent());
}

function fileObj(id,type,field){
	this.id=id;
	this.type=type;
	this.field = field;
}
function loadBreviaryPic(arrNot){
	 arrFirstPic=[];
	//如果有附件
	if(file_data && file_data.length>0){
		var len = file_data.length;
		for(var i=0;i<len;i++){
			var blflag = true;
			for(var k=0;k<arrNot.length;k++){
				if(file_data[i].file_fieldname==arrNot[k]){
					blflag = false;
					break;
				}
			}
			if(blflag){
				var file_name = file_data[i].file_name;
				var id = file_data[i].id;
				var temp = $("<li onclick='show_pic(\""+id+"\",\""+file_data[i].file_type+"\")' name='"+checked_tab+"' style='display:inline; width:80px; height:80px; float:left; margin:10px 10px 10px 10px; border:1px solid #444; cursor:pointer;'><img  style='display:block; width:80px; height:80px;' src='"+contextPath+"/imageDownloadServlet.srv?imgFileID="+id+"' title='"+file_name+"' alt='"+file_name+"' /></li>");
				$("#li_"+file_data[i].file_fieldname).append(temp);
				//将每个标签页的第一个图片的信息保存到arrFirstPic数组
				var fieldflag = false;
				for(var j=0;j<arrFirstPic.length;j++){
					if(arrFirstPic[j].field==file_data[i].file_fieldname){
						fieldflag=true;
						break;
					}
				}
				if(fieldflag==false){
					arrFirstPic.push(new fileObj(id,file_data[i].file_type,file_data[i].file_fieldname));
				}
			}
		}
	}
}
//如果某个tab页对应的备注为空，则不显示备注
function setRemark(data){
	for(var pro in data){
		if(pro.substring(pro.length-6).toLowerCase()=="remark"){
			if(data[pro]==""){
				$("#"+pro).remove();
			}
		}
	}
}

					
	//单击缩略图显示大图信息
function show_pic(id,file_type){
	var file_name = $('#tt').tabs('getSelected').attr("name_file");
	$("#"+file_name).css("display","inline");
	$("#"+file_name).attr("src",contextPath+'/imageDownloadServlet.srv?imgFileID='+id);
	$("#"+file_name).attr("onclick","javascript:viewImage('"+id+"','"+file_type+"')");
	$("#"+file_name).css("cursor","pointer");
	
}

function getOrderNo(){
	var orderNo = new Date().getTime();
	//orderNo = orderNo + "" + Math.floor(Math.random()*100);
	return orderNo;
}

function getCoordinate(type_temp) {
	var len_coordinate = coordinate_data.length;
	//先移除图片上的热点
	$("area").remove();
	//进行坐标初始化
	for ( var i = 0; i < len_coordinate; i++) {
		if(coordinate_data[i].type==type_temp){
			msg = parseInt(coordinate_data[i].coor_name);
			//6：向前台传入坐标，坐标类型
			myDrawCanven(coordinate_data[i].x_coordinate,coordinate_data[i].y_coordinate,coordinate_data[i].type);
			//添加热点
			add_hyperlink(coordinate_data[i].x_coordinate,coordinate_data[i].y_coordinate,coordinate_data[i].type,coordinate_data[i].length,coordinate_data[i].coor_name);
		}
	}
}

//添加热点连接
function add_hyperlink(x,y,type,length,order){
	var link_area;
	if(type=="1"){
		//添加圆形热点
		link_area= $("<area shape='circle' coords='"+x+","+y+","+length+"' href ='javascript:get_file("+order+","+type+")'/>");
	}else if(type=="2"){
		//添加矩形热点，并做微调
		/*var x1=(parseInt(x)-3);
		var y1=(parseInt(y)-3);
		var x2=x1+parseInt(length);
		var y2=y1+parseInt(length);
		link_area = $("<area shape='rect' coords='"+x1+","+y1+","+x2+","+y2+"' href ='javascript:get_file("+order+","+type+")'/>");
	*/
		var tempX = x-0;
		var tempY = y-0;
		//三角形三个顶点
		var x1 = tempX-8;
		var y1 = tempY-8;
		var x2 = tempX+8;
		var y2 = tempY-8;
		var x3 = tempX+8;
		var y3 = tempY+8;
		var x4 = tempX-8;
		var y4 = tempY+8;
		link_area= $("<area shape='polygon' coords='"+x1+","+y1+","+x2+","+y2+","+x3+","+y3+","+x4+","+y4+"' href ='javascript:get_file("+order+","+type+")'/>");
	}else if (type=="3"){
		//添加三角形
		var tempX = x-0;
		var tempY = y-0;
		
		var x1 = tempX;
		var y1 = tempY-8;
		var x2 = tempX+10;
		var y2 = tempY+8;
		var x3 = tempX-10;
		var y3 = tempY+8;
		link_area= $("<area shape='polygon' coords='"+x1+","+y1+","+x2+","+y2+","+x3+","+y3+"' href ='javascript:get_file("+order+","+type+")'/>");
	}
	$("#Map").append(link_area);
}

//初始化控制点线定义位图
//使用前请先在页面定义全局contextPath
function initImg(){
	var fileid = data.imgFileID;
	if(typeof fileid=="undefined"||fileid==""){
		$("#cd_file").attr("src", "none");
		$("#cd_file").attr("alt","控制点线定义位置图未上传");
	}else{
		$("#cd_file").attr("src", "none");
		$("#cd_file").attr("src", contextPath+'/imageDownloadServlet.srv?imgFileID='+fileid);
	}
}
//清空画板
function clearCanvas() {
	$("#canvas>div").remove();
}

function addFileInputAndDraw(tablename) {
	//控制点中的必填项
	//基准面静校正:静校正前单炮,基准面静校正:静校正后单炮,噪声压制:去噪后单炮,子波处理:反褶积后单炮频谱,
	var bombarr = new Array(new Array("gp_proc_base_adjust","pre_bomb_file"),new Array("gp_proc_base_adjust","post_bomb_file"),new Array("gp_proc_noisy_pression","post_bomb_file"),new Array("gp_proc_subwave","post_bomb_file"));
	//控制线中的必填项
	//	1：基准面静校正:静校正前初叠加,								1：基准面静校正:静校正后初叠加,							2：噪声压制:去噪后叠加剖面,											2：子波处理:反褶积后叠加剖面,						3：水平叠加：叠加剖面								3：叠后偏移：偏移速度剖面                                               					 4：叠前时间偏移：叠前时间偏移剖面							 4：叠前深度偏移：叠前深度偏移剖面
	var sectionarr = new Array(new Array("gp_proc_base_adjust","pre_overlap_file"),new Array("gp_proc_base_adjust","post_overlap_file"),new Array("gp_proc_noisy_pression","post_overlap_section_file"),new Array("gp_proc_subwave","post_compare_file"),new Array("gp_proc_horizantal_added","overlap_section_file"),new Array("gp_proc_after_added_offset","offse_speed_file"),new Array("gp_proc_bef_added_time_offset","pre_time_section_file"),new Array("gp_proc_bef_added_deep_offset","deepth_section_file"),new Array("gp_proc_after_added_offset","offset_archievement_file"),new Array("gp_proc_after_added_proc","section_scan_file"));
	//4：得到坐标条数
	var lenCoordinate = coordinate_data.length;
	//动态拼出附件上传域
	for ( var i = 0; i < lenCoordinate; i++) {
		if(coordinate_data[i].type==1){
			$("div[name='bomb']>table").each(function(){
				var name =  $(this).attr("stat");
				var object  = $("<tr><td class='inquire_item'>"+name+coordinate_data[i].coor_name+"：</td><td class='inquire_form'  id='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'><input class='easyui-validatebox textfield9' validType='v_img' type='file' name='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'/></td></tr>"); ;
				if(typeof tablename != "undefined" ||  tablename!=""){
					for(var j=0;j<bombarr.length;j++){
						if(tablename==bombarr[j][0]&&$(this).attr("name_file")==bombarr[j][1]){
							 object = $("<tr><td class='inquire_item'><font title='红色*表示此项必填'color='red'>* </font>"+name+coordinate_data[i].coor_name+"：</td><td class='inquire_form'  id='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'><input class='easyui-validatebox textfield9' required validType='v_img' type='file' name='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'/></td></tr>");
						}
					}
				}
		 		$(this).append(object);
		 		$.parser.parse(this);
			});
		}
		if(coordinate_data[i].type==2){
			$("div[name='section']>table").each(function(){
				var name = $(this).attr("stat");
				var object = $("<tr><td class='inquire_item'>"+name+coordinate_data[i].coor_name+"：</td><td class='inquire_form'  id='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'><input class='easyui-validatebox textfield9' validType='v_img' type='file' name='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'/></td></tr>");
				if(typeof tablename != "undefined" ||  tablename!=""){
					for(var j=0;j<sectionarr.length;j++){
						if(tablename==sectionarr[j][0]&&$(this).attr("name_file")==sectionarr[j][1]){
							object = $("<tr><td class='inquire_item'><font title='红色*表示此项必填'color='red'>* </font>"+name+coordinate_data[i].coor_name+"：</td><td class='inquire_form'  id='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'><input class='easyui-validatebox textfield9' required validType='v_img' type='file' name='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'/></td></tr>");
						}
					}
				}
				$(this).append(object);
				$.parser.parse(this);
			});
		}
		if(coordinate_data[i].type==3){
			$("div[name='gather']>table").each(function(){
				var name = $(this).attr("stat");
				var object = $("<tr><td class='inquire_item'>"+name+coordinate_data[i].coor_name+"：</td><td class='inquire_form'  id='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'><input class='easyui-validatebox textfield9' validType='v_img' type='file' name='"+$(this).attr('name_file')+'__'+coordinate_data[i].coor_name+"'/></td></tr>");
				$(this).append(object);
				$.parser.parse(this);
			});
		}
		//5：得到当前坐标的名字（序号）
		//msg = parseInt(coordinate_data[i].coor_name);
		//6：向前台传入坐标，坐标类型
		//drawCanven(coordinate_data[i].x_coordinate,coordinate_data[i].y_coordinate,coordinate_data[i].type);
	}
}

function myDrawCanven(mouseX, mouseY, flag) {
	//得到画板对象
	var canvasDiv = document.getElementById("canvas");
	//创建一个新div
	gr = new jsGraphics(canvasDiv);
	var penWidth;
	var col;
	var pen;
	setPenColor(true);
	var point;
	//坐标div
	var pointDiv;
	//画点
	drawPoint();
	//设置颜色
	function setPenColor(noAlert) {
		col = new jsColor("blue");
		penWidth = '2';
		if (!isNaN(penWidth))
			pen = new jsPen(col, penWidth);
		else
			pen = new jsPen(col, 1);
		return true;
	}
	//画点
	function drawPoint() {
		//如果有画点div移除div
		try {
			if (pointDiv)
				canvasDiv.removeChild(pointDiv);
		} catch (e) {
		}
		//得到当前坐标div 大小
		pointDiv = gr.fillRectangle(new jsColor("green"), new jsPoint(
				mouseX - 3, mouseY - 3), 6, 6);
		//得到当前坐标点位置
		point = new jsPoint(mouseX, mouseY);
		//得到当前坐标类型
		var flagtemp = flag;
		if (flagtemp == 1) {
			//画圆
			drawCircle();
		}
		if (flagtemp == 2) {
			//画矩形
			drawRectangle();
		}
		if (flagtemp == 3) {
			//画三角形
			drawTrigon();
		}
	}
	function ShowPoints() {
		txt.value = "";
		for ( var i = 0; i < points.length; i++) {
			txt.value = txt.value + "new jsPoint(" + points[i].x + ","
					+ points[i].y + "),";
		}
	}
	//画圆
	function drawCircle() {
		var radius = '10';
		if (!setPenColor())
			return;
		gr.drawCircle(pen, point, radius);
		drawText();
	}
	//画三角形
	function drawTrigon(){
		if (!setPenColor())
			return;
		var tempX =  parseInt(mouseX);
		var tempY = parseInt(mouseY);
		//三角形三个顶点
		var x1 = tempX;
		var y1 = tempY-8;
		var x2 = tempX+10;
		var y2 = tempY+8;
		var x3 = tempX-10;
		var y3 = tempY+8;
		gr.drawPolygon(pen, new Array(new jsPoint(x1,y1),new jsPoint(x2,y2),new jsPoint(x3,y3)));
		drawText();
	}
	//画椭圆
	function drawEllip(){
		if (!setPenColor())
			return;
		gr.drawEllipse(pen, point, 20, 10);
		drawText();
	}
	//移除点
	function removpoint() {
		var length = $("#canvas").children().size();
		$("#canvas div:eq(" + length + ")").remove();
	}
	//画矩形
	function drawRectangle() {
		if (!setPenColor())
			return;
		//矩形的1/2宽高
		var len = 8;
		var tempX = parseInt(mouseX);
		var tempY = parseInt(mouseY);
		//矩形四个顶点
		var x1 = tempX-len;
		var y1 = tempY-len;
		var x2 = tempX+len;
		var y2 = tempY-len;
		var x3 = tempX+len;
		var y3 = tempY+len;
		var x4 = tempX-len;
		var y4 = tempY+len;
		gr.drawPolygon(pen, new Array(new jsPoint(x1,y1),new jsPoint(x2,y2),new jsPoint(x3,y3),new jsPoint(x4,y4)));
		drawText();
	}
	//输入文字
	function drawText() {
		var width = 10;
		var text = msg;
		if (!text || !point)
			return false;
		if (width != null)
			width = Math.round(width) + "px";
		var textDiv = canvasDiv.appendChild(document.createElement("div"));
		textDiv.style.position = "absolute";
		//为了使文本一直处于图形中心做适当调整
		var tempx = phPoint.x;
		if(text>9){
			tempx=tempx-3;		
		}
		var tempy = phPoint.y -3;
		textDiv.style.left = tempx + "px";
		textDiv.style.top = tempy + "px";
		if (width)
			textDiv.style.width = width;
		textDiv.innerHTML = "<a style='font-weight:bold;font:10;color:#DC143C' href='javascript:get_file("+msg+","+flag+")'>"+text+"</a>";
		return textDiv;
	}
}

//通过热点连接得到附件信息，通过质控点ID，附件字段名称，附件顺序，附件类型得到唯一附件信息
//使用前请现在页面脚本中定义全局的staticWeb，和contextPath，checked_tab，prj_id
function get_file(order,type){
	var tab_name = $("#tab_name").val();
	$("#"+checked_tab).css("display","inline");
	//设置loading动画
	$("#"+checked_tab).attr("src",staticWeb+"/images/loadingpic.gif" );
	$.ajax({
		url: contextPath+"/proc/progress/getFileByCoordinate.srq",
		type : "POST",
		data : {"type" : type,"order" :order,"prjId":prj_id,"fileName":checked_tab,"tablName":tab_name},
		success : function(data) {
			//记录每个标签页对应的附件的order
			$("#"+checked_tab+"_order").val(order);
			//通过得到的附件图片url拼接到相应的tab下img元素中
			var res = jQuery.parseJSON(data);
			if (res.flag) {
				var id = res.id;
				$("#"+checked_tab).attr("src",contextPath+'/imageDownloadServlet.srv?imgFileID='+res.id);
				$("#"+checked_tab).attr("onclick","javascript:viewImage('"+id+"','"+res.file_type+"')");
				$("#"+checked_tab).css("cursor","pointer");
			} else {
				$("#"+checked_tab).attr("src","none" );
				$("#"+checked_tab).attr("alt","附件未上传" );
			}	
		}
	});
	//当显示的是原始资料分析中的频率特征或者原始单炮，存储对应的order
	if(tab_name=="GP_PROC_ORIENAL_DATA_ANYLYSIS"){
		if(checked_tab=="freq_character_file"||checked_tab=="first_cannon_record_file"){
			tempOrder=order;
		}
	}
	//子波处理
	if(tab_name=="GP_PROC_SUBWAVE"){
		if("pre_bomb_file"==checked_tab||"post_bomb_file"==checked_tab){
			bombOrder=order;
		}
		if("pre_compare_file"==checked_tab||"post_compare_file"==checked_tab){
			sectionOrder=order;
		}
	}
	//叠后偏移
	if(tab_name=="GP_PROC_AFTER_ADDED_PROC"){
		if(checked_tab=="section_scan_file"||checked_tab=="post_section_file"||"pre_section_file"==checked_tab){
			sectionOrder=order;
		}
	}
	//基准面静校正
	if(tab_name=="GP_PROC_BASE_ADJUST"){
		 if(checked_tab=="pre_bomb_file"||checked_tab=="post_bomb_file"){
			bombOrder=order;
		 }
		 if(checked_tab=="pre_overlap_file"||checked_tab=="post_overlap_file"){
			sectionOrder=order;
		 }
	}
	//噪声压制
	if(tab_name=="GP_PROC_NOISY_PRESSION"){
		 if(checked_tab=="pre_bomb_file"||checked_tab=="post_bomb_file"){
			 bombOrder=order;
		 }
		 if(checked_tab=="pre_overlap_section_file"||checked_tab=="post_overlap_section_file"){
			 sectionOrder=order;
		 }
	}
	//振幅补偿
	if(tab_name=="GP_PROC_COMPA_HANDLING"){
		if(checked_tab=="pre_bomb_file"||checked_tab=="post_bomb_file"){
			 bombOrder=order;
		 }
		 if(checked_tab=="pre_section_file"||checked_tab=="post_section_file"){
			 sectionOrder=order;
		 }
	}
	//剩余静校正
	if(tab_name=="GP_PROC_LEFT_ADJUST"){
		if("pre_cmp_set_file"==checked_tab||"post_cmp_set_file"==checked_tab){
			 cmpOrder=order;
		}
		if("pre_overlap_section_file"==checked_tab||"post_overlap_section_file"==checked_tab){
			 sectionOrder=order;
		}
	}
	//叠后偏移
	if(tab_name=="GP_PROC_AFTER_ADDED_OFFSET"){
		if("offse_speed_file"==checked_tab||"offset_archievement_file"==checked_tab){
			 sectionOrder=order;
		}
	}
	
	//叠前时间偏移
	if(tab_name=="GP_PROC_BEF_ADDED_TIME_OFFSET"){
		if("overlap_section_file"==checked_tab||"post_offset_section_file"==checked_tab||"pre_time_section_file"==checked_tab||"pre_time_speed_section_file"==checked_tab){
			 sectionOrder=order;
		}
	}
	//叠后深度偏移
	if(tab_name=="GP_PROC_BEF_ADDED_DEEP_OFFSET"){
		if("time_offset_section_file"==checked_tab||"deepth_section_file"==checked_tab||"speed_section_file"==checked_tab){
			 sectionOrder=order;
		}
	}
}
function downFile(field, fid, id, suffix, filepath) {
	window.location.href = requestContextPath+"/largeFileDownload.srv?fileID="
			+ fid;
}
function viewFile(field,fid,id,suffix,filepath){
	//局部放大功能调用
	//fie：文件ID  suffix：后缀      true：可编辑/false：不可编辑      web context    是否是整体放大缩小功能
	$.viewFile(fid,suffix,true,requestContextPath,false);
	//整体放大、缩小功能调用
	//$.viewFile(fid,suffix,false,"${pageContext.request.contextPath}",true);
}
function downFile(field,fid,id,suffix,filepath){
	window.location.href=requestContextPath+"/largeFileDownload.srv?fileID="+fid;
}
function viewImage(fid,suffix){
	//局部放大功能调用
	//fie：文件ID  suffix：后缀      true：可编辑/false：不可编辑      web context    是否是整体放大缩小功能
	$.viewFile(fid,suffix,true,requestContextPath,false);
	//整体放大、缩小功能调用
	//$.viewFile(fid,suffix,false,"${pageContext.request.contextPath}",true);
}
